package com.cowin.sys.service.impl;

import com.cowin.inf.model.vo.JphHHVo;
import com.cowin.sys.dao.TcSysCodeListMapper;
import com.cowin.sys.dao.TeOfmVehiclePlanMapper;
import com.cowin.sys.dao.TmVhcVehicleMovementMapper;
import com.cowin.sys.model.TeOfmVehiclePlan;
import com.cowin.sys.model.TmVhcVehicleMovement;
import com.cowin.sys.model.vo.DateFormVo;
import com.cowin.sys.model.vo.OutPutVo;
import com.cowin.sys.model.vo.IdNameVo;
import com.cowin.sys.model.vo.VehicleMovementVo;
import com.cowin.sys.service.TmVhcVehicleMovementService;
import com.cowin.base.AbstractServiceImpl;
import com.cowin.utils.Arith;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Condition;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;


/**
 * Created by  on 2019/10/29.
 *
 * @author CodeGenerator
 */
@Slf4j
@Service
@Transactional(rollbackFor = RuntimeException.class)
public class TmVhcVehicleMovementServiceImpl extends AbstractServiceImpl<TmVhcVehicleMovement> implements TmVhcVehicleMovementService {

    @Resource
    private TmVhcVehicleMovementMapper tmVhcVehicleMovementMapper;

    @Resource
    private TeOfmVehiclePlanMapper teOfmVehiclePlanMapper;

    @Resource
    private TcSysCodeListMapper tcSysCodeListMapper;

    @Override
    public List<Map<Integer, Integer>> findDayOutput(OutPutVo outPutVo) {
        return tmVhcVehicleMovementMapper.findDayOutput(outPutVo);
    }

    @Override
    public List<Map<String, Integer>> findScopeOutput(OutPutVo outPutVo) {
        return tmVhcVehicleMovementMapper.findScopeOutput(outPutVo);
    }

    @Override
    public List<Map<String, Object>> findPointPositionForm(TeOfmVehiclePlan teOfmVehiclePlan) {
        /*
        查询生产批次号
         */
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("1 = 1");
        if (StringUtils.isNotBlank(teOfmVehiclePlan.getYear())) {
            stringBuilder.append("and YEAR = '" + teOfmVehiclePlan.getYear() + "'");
        }
        if (StringUtils.isNotBlank(teOfmVehiclePlan.getMonth())) {
            stringBuilder.append("and MONTH = '" + teOfmVehiclePlan.getMonth() + "'");
        }
        if (StringUtils.isNotBlank(teOfmVehiclePlan.getWeek())) {
            stringBuilder.append("and WEEK = '" + teOfmVehiclePlan.getWeek() + "'");
        }
        if (StringUtils.isNotBlank(teOfmVehiclePlan.getBatchNo())) {
            stringBuilder.append("and BATCH_NO = '" + teOfmVehiclePlan.getBatchNo() + "'");
        }
        if (StringUtils.isNotBlank(teOfmVehiclePlan.getLineNo())) {
            stringBuilder.append("and LINE_NO = '" + teOfmVehiclePlan.getLineNo() + "'");
        }
        if (StringUtils.isNotBlank(teOfmVehiclePlan.getVehicleType())) {
            stringBuilder.append("and VEHICLE_TYPE = '" + teOfmVehiclePlan.getVehicleType() + "'");
        }
        Condition teOfmVehiclePlanCondition = new Condition(TeOfmVehiclePlan.class);
        teOfmVehiclePlanCondition.createCriteria().andCondition(stringBuilder.toString());
        List<TeOfmVehiclePlan> teOfmVehiclePlanList = teOfmVehiclePlanMapper.selectByCondition(teOfmVehiclePlanCondition);
        //查询 批次集合的所有订单数量(总数量)对应车辆的TM_VHC_VEHICLE_ID
        List<Integer> list = new ArrayList<>();
        if (teOfmVehiclePlanList != null && teOfmVehiclePlanList.size() > 0) {
            list = teOfmVehiclePlanMapper.findVehicleIdBySapOrderNo(teOfmVehiclePlanList);
        }

        //获取车间点位 焊装10 20 涂装 10 20  总装 10 20 30
        List<IdNameVo> tmBasUlocId = tcSysCodeListMapper.getTmBasUlocId();
        //循环遍历点位信息
        List<Integer> finalList = list;
        List<Map<String, Object>> resultList = new ArrayList<>();
        tmBasUlocId.forEach(e -> {
            //根据工位获取过点车辆的TM_VHC_VEHICLE_ID
            List<Integer> tmVhcVehicleIdByTmBasUlocIds = tmVhcVehicleMovementMapper.getTmVhcVehicleIdByTmBasUlocId(e.getId());
            //对比订单获取的 和 根据工位获取的
            Integer count = intersection(finalList, tmVhcVehicleIdByTmBasUlocIds);
            //封装返回结果数据
            Map<String, Object> map = new HashMap<>();
            map.put("name", e.getName());
            map.put("unfinished", finalList.size() - count);
            map.put("accomplish", count);
            resultList.add(map);
        });
        return resultList;
    }

    @Override
    public List<Integer> getTmVhcVehicleIdByTmBasUlocId(Integer tmBasUlocId) {
        return tmVhcVehicleMovementMapper.getTmVhcVehicleIdByTmBasUlocId(tmBasUlocId);
    }


    public Integer intersection(List<Integer> list1, List<Integer> list2) {
        HashSet<Integer> set1 = new HashSet<Integer>();
        for (Integer n : list1) {
            set1.add(n);
        }
        HashSet<Integer> set2 = new HashSet<Integer>();
        for (Integer n : list2) {
            set2.add(n);
        }

        set1.retainAll(set2);
        Integer total = 0;
        for (Integer s : set1) {
            total += 1;
        }
        return total;
    }

    @Override
    public List<JphHHVo>  getRealityJph(String ulocId, Date time) {
        List<JphHHVo> realityJph = tmVhcVehicleMovementMapper.getRealityJph(ulocId, time);
        Map<String, String> collect = realityJph.stream().collect(Collectors.toMap(JphHHVo::getHh, JphHHVo::getTotal));
        //判断是否是同一天
//        boolean sameDay = DateUtils.isSameDay(time, new Date());
        List<JphHHVo> result =new ArrayList<>();
//        if (sameDay) {
//            Calendar c = Calendar.getInstance();
//            int hour = c.get(Calendar.HOUR_OF_DAY);
//            for(int i=0;i<=hour;i++){
//                realityJph.forEach(e->{
//
//                });
//            }
//        }else {

//        }

        for(int i=0;i<=23;i++){
            JphHHVo jphHHVo = new JphHHVo();
            if(i<10){
                jphHHVo.setHh("0"+i);
                jphHHVo.setTotal(collect.get("0"+i)==null?"0":collect.get("0"+i));
            }else {
                jphHHVo.setHh(""+i);
                jphHHVo.setTotal(collect.get(""+i)==null?"0":collect.get(""+i));
            }
            result.add(jphHHVo);
        }
        return result;
    }

    @Override
    public List<DateFormVo> getDateForm(String dateTime) {
        return tmVhcVehicleMovementMapper.findDateForm(dateTime);
    }

    @Override
    public List<VehicleMovementVo> getVehicleMovementInfo(VehicleMovementVo vehicleMovementVo) {
        return tmVhcVehicleMovementMapper.findVehicleMovementInfo(vehicleMovementVo);
    }

    @Override
    public List<String> getVinHasRecordByUlocId(List<String> vins, String ulocNo) {
        return tmVhcVehicleMovementMapper.findVinHasRecordByUlocId(vins, ulocNo);
    }
}
